import { defineStore } from 'pinia'
let tags = localStorage.getItem('tags')
let taglists: any = null
if (tags) {
  taglists = JSON.parse(tags)
}
export const tagstore = defineStore({
  id: 'tags',
  state: () => {
    return {
      tags: taglists?.tags || [{
        path: '/homeview',
        name: 'homeview',
        title: '首页'
      }],
    }
  },
  getters: {
  },
  actions: {
    addtags(tag: any) {
      var index = this.tags.findIndex(item => item.name=== tag.name)
      if(index==-1){
        this.tags.push(tag)
      }
    },
    closetags(ftag: any) {
      this.tags.map((item: any, index: any) => {
        if (item.name == ftag.name) {
          this.tags.splice(index, 1);
        }
      })
    },
  },
  // 开启数据缓存
  persist: { //数据默认存在 sessionStorage 里，并且会以 store 的 id 作为 key
    enabled: true,
    strategies: [
      {
        key: 'tags',
        storage: localStorage,
      }
    ]
  }
})
